<template>
    <view class="pub_page pub_page_bg pub_address pub_header_padding">
        <pub-head headerName="address" :headerConfig="headerConfig" @headerBack="headerBack"></pub-head>
        <pub-dialog v-show="dialogShow" dialogName="address" :dialogConfig="dialogConfig" @dialogBack="dialogBack"></pub-dialog>

        <z-paging ref="Zpaging" class="lis" v-model="list" @query="getList" :refresher-enabled="false" :fixed="false">
            <div class="li" v-for="(item,key) in list" :key="key" @click="chooseAddress(item)">
                <div class="p_1">{{item.receiver}}&nbsp;&nbsp;&nbsp;&nbsp;{{item.phone}}</div>
                <div class="p_2">{{item.area}}{{item.address}}</div>
                <div class="btn" v-show="openType==1">
                    <div class="def"><span class="icon-font-basic" :class="item.isDefault==1?'icon-b-select-6':'icon-b-select-4'"></span>默认</div>
                    <div class="del" @click.stop="delAddress(item,key)">删除</div>
                    <div class="edit" @click.stop="$comFun.openWin('/pages/other/address_info', {info:JSON.stringify(item)})">编辑</div>
                </div>
            </div>
        </z-paging>
    </view>
</template>

<script>
export default {
    data() {
        return {
            headerConfig: {
                title: '',//标题栏内容
                left_type: 3,//左侧区域类型  0不出现  1文字  2图片  3返回按钮
                right_type: 1,//右侧区域类型  0不出现  1文字  2图片
                right_content: '新增',//右侧内容  right_type为1时文字内容 为2时图片链接
                right_color: '#fff',//右侧文字颜色
            },

            list: [],
            openType: 0,//1我得地址列表  2选择地址

            dialogShow: false,
            dialogConfig: {
                content: '是否确定删除当前地址？'
            },
            delInfo: ''
        };
    },
    onLoad(option) {
        this.openType = option.openType;
        this.headerConfig.title = this.openType == 1 ? '收货地址' : '选择收货地址';
        setTimeout(() => {
            this.$comFun.openLoading();
            this.$refs.Zpaging.reload()
        }, 400);
        const that = this;
        uni.$on('refreshAddressPage', function () {
            that.$refs.Zpaging.reload();
        })
    },
    methods: {
        headerBack(data) {
            // console.log(data);
            if (data.type == 'right') {
                this.$comFun.openWin('/pages/other/address_info');
            }
        },
        async getList(pageNo, pageSize) {
            const res = await this.$request.get('address/myList', {
                pageNo: pageNo,
                pageSize: pageSize,
            });
            this.$comFun.closeLoading();
            // console.log(res);
            if (res.code == 200) {
                this.$refs.Zpaging.complete(res.result.records);
            } else {
                this.$refs.Zpaging.complete(false);
            }
        },
        delAddress(info, key) {
            this.delInfo = {
                key: key,
                id: info.id,
            }
            this.dialogShow = true;
        },
        async dialogBack(bdata) {
            this.dialogShow = false;
            if (bdata.name == 'address' && bdata.key == 1) {
                this.$comFun.openLoading();
                const res = await this.$request.get('address/delete', {
                    id: this.delInfo.id
                });
                // console.log(res);
                this.$comFun.closeLoading();
                this.$comFun.toast(res.message);
                if (res.code == 200) {
                    this.list.splice(this.delInfo.key, 1);
                    this.delInfo = '';
                }
            }
        },
        chooseAddress(info) {
            if (this.openType == 2) {
                uni.$emit('chooseAddressBack', info);
                this.$comFun.closeWin();
            }
        }
    },
};
</script>

<style lang="scss" scoped>
@import "@/common/css/other.scss";
</style>
